package com.uottawa.library.controller;

import java.io.IOException;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uottawa.library.domain.Item;
import com.uottawa.library.service.ItemService;
 
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name="/ItemServlet",  urlPatterns={"/addItem.do", "/removeItem.do", "/updateItem.do", "/items.do", "/showItem.do" })

public class ItemServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ItemService itemService = new ItemService();
	
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
 
        String isbn = request.getParameter("isbn");
        String id = request.getParameter("id");
        
        if(isbn.isEmpty()) {
        	 request.setAttribute("error", "all fields are required");
        	 request.getRequestDispatcher("/item.jsp").forward(request, response);
        	 return;
        }
        String url = request.getServletPath();
        if(url.contains("add")) {
        	String result = itemService.addItem(Integer.parseInt(isbn));
            request.setAttribute("result", result);
        } else if(url.contains("update")) {
        	String result = itemService.updateItem(Integer.parseInt(id), Integer.parseInt(isbn));
            request.setAttribute("result", result);
            List<Item> items = itemService.getAllItems();
			request.setAttribute("items", items);
        }
       
        
    	request.getRequestDispatcher("/item.jsp").forward(request, response);

 
    }
    
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    	
    	String url = request.getServletPath();
    	
    	if(url.contains("remove")) {
    		String id = request.getParameter("id");
    		if(id != null) {
    			String result = itemService.removeItem(Integer.parseInt(id));
    			request.setAttribute("result", result);			
    		} else {
    			List<Item> items = itemService.getAllItems();
    			request.setAttribute("items", items);			
    		}
    	} else if(url.contains("update")){
    		List<Item> items = itemService.getAllItems();
			request.setAttribute("items", items);	
    	} 
    	else if(url.contains("show")){
    		String isbn = request.getParameter("isbn");
    		if(isbn !=null)
    			{
    			int isbnInt = Integer.parseInt(isbn);
    			
    		List<Item> items = itemService.getAllAvailableItems(isbnInt);
			request.setAttribute("items", items);
    			}
    		else
    			request.setAttribute("result", "Please choose the title ...");
    		
    	} 	
    	request.getRequestDispatcher("/item.jsp").forward(request, response);

 
    }
}